Skip to main content

Getting Started with pytest-bdd-ewx

Welcome to pytest-bdd-ewx ! This framework extends the power of PyTest BDD plugin to provide the capabilities to create test scenarios for the Energyworx platform.

🏗 Create a test project

  1. To begin, create your test project with the following structure:
|-  my-test-project
|- my_test_scenarios
...
|- resources
  • my_test_scenarios will contain the actual tests, it can be also multiple folders containing tests for different use cases
  • resources folder will contain some test data, please see more information below
  1. Make sure you have created a virtual environment and installed pytest-bdd-ewx package. Installation guide can be found here: Energyworx Sharable Packages To use the plugin, please make sure you executed the command:
export PYTEST_PLUGINS=pytest_bdd_ewx.plugin

Now you can start writing your tests using the Gherkin syntax (if you are unfamiliar with it, please refer to the documentation for more details about the syntax).

📂 Add test files

Create a .feature file containing one or multiple scenarios that you want to test. For convenience, a dedicated features folder can be created. To execute the test, you will need to create a pytest .py file with the scenario-decorated functions. See example:

|-  my-test-project
|- my_test_scenarios
|- features
|- test_ingestion.feature
...
|- test_ingestion.py
...

Feature file will have a Feature described and a Scenario written in Gherkin syntax utilising the steps defined in pytest-bdd-ewx plugin.

For more information, please refer to Using the plugin

🛠️ Create a simple test

Example: test_ingestion.feature

Feature: File ingestion
As a QA engineer
I want to ingest the file
In order created a datasource

Scenario: The datasource is created via file ingestion
Given I upload the file "test_data.csv"
When I ingest the file "test_data.csv" with market adapter "csv_market_adapter"
Then there is a datasource with id "TEST_DATASOURCE"

Corresponding test_ingestion.py file:

from pytest_bdd import scenario


@scenario("features/test_ingestion.feature", "The datasource is created via file ingestion")
def test_ingestion():
pass

This way, pytest-bdd-ewx will seamlessly integrate with pytest to discover and execute your BDD tests.

💡 For more information about available steps in the plugin, please refer to Available Gherkin Steps documentation.

📜 Add test data

We distinguish 2 types of test resources management:

  1. Test data management: please note the test_data.csv file location in the resources folder. The folder structure should be exactly as the following, otherwise the plugin will not be able to find it:
|-  my-test-project
|- my_test_scenarios
|- features
...
|- resources
|- test-data
|- files
|- test_data.csv
|- another_test_data.json
...

The resources folder is also used for some more advanced scenarios test data, see Using the plugin for more information.

  1. Test configurations management: in case your market adapter or some other configurations for your test (like flows, rules, etc.) are not present on the target namespace, it is possible to create those as a precondition to your test scenarios. For seamless configuration management, we recommend utilising ewx-cli energyworx tool which helps with both downloading and uploading the needed configurations for the specific test case. Please refer to the last section in Using the plugin on how to set it up.

Supposing you have your test configurations on the target namespace, you can execute your tests.

🚀 Start testing

Now you can start your test by the following command:

pytest my_test_scenarios/test_ingestion.py

To have more logging information regarding the tests execution, you can run the following command:

pytest --log-cli-level=INFO my_test_scenarios/test_ingestion.py -x

When the test is started, you will be suggested to specify an environment and a target namespace to run the tests. Follow the instructions in order to authenticate your user to the platform (normally you will need to do it only once after you start testing on a new environment).

It is also possible to create your own custom step definitions, if needed. For more information on how to create the tests and utilise the plugin, please refer to the Using the plugin

⚙️ Environment variables

Optionally, you can set up environment variables, then the test will run against the specified environment on the specified namespace.

VariableExample valueDescription
AUTOMATED_TESTS_NAMESPACE_IDenrx_org_001All the api calls are done within this namespace.
AUTOMATED_TESTS_ENVIRONMENTewx-test-projectGCP project ID of the desired environment.
URL_BASE_PROJECT_IDtest.staging-projectOptional, only needed if the project ID and the base URL are not matching.

⚠️ Execute test against API V2

Note that there is currently no capability in ewx-cli tool to manage configurations using API version 2 (which is used in new console). It also concerns authentication, so in order to run the test against API V2, you need to set up the following variables:

  • AUTOMATED_TESTS_APIV2_ID
  • AUTOMATED_TESTS_APIV2_KEY

To get these values for your user, please contact support team. Once it is set, you can use the following step to start testing in API V2:

Given I switch to api version "2"

We are currently in process of implementing existing test steps using new endpoints in API V2, some of the steps are already available.

If you switch in your scenario to API V2, available steps will run using endpoints in API V2, and if the implementation is not yet in place, the test will make use of endpoints in API version 1.5 (the ones used in the original console). It is possible to switch your scenario back to API V1.5 explicitly by:

Given I switch to api version "1.5"

Please note that some steps parameters can have a different format depending on API version used, which potentially will require some modifications in the steps values to make it work in API V2. You should get a proper error message in case some parameter doesn't much the expected format when executing a test.

If you have any questions, please contact our support and QA team.

🧑‍💻 Happy testing!